﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace ProceduralCity
{
    class Input
    {
        FPSCamera camera; // TODO: instead of having access to a camera, this should probably have access to the entire object set
        KeyboardState prevKeyboard;
        bool captureMouse = false;
        public bool CaptureMouse
        {
            get { return captureMouse; }
        }

        public Input(FPSCamera camera)
        {
            this.camera = camera;
        }

        public void Update(float deltaSeconds)
        {
            KeyboardState keyboard = Keyboard.GetState();

            // Allows the game to exit
            if (keyboard.IsKeyDown(Keys.Escape))
                Game.RequestExit();

            // Mouse capture
            bool prevCaptureMouse = captureMouse;
            if (keyboard.IsKeyDown(Keys.C) && !prevKeyboard.IsKeyDown(Keys.C))
                captureMouse = !captureMouse;

            // Camera movement
            if (keyboard.IsKeyDown(Keys.W))
                camera.MoveForwardInPlane(deltaSeconds);
            if (keyboard.IsKeyDown(Keys.S))
                camera.MoveForwardInPlane(-deltaSeconds);
            if (keyboard.IsKeyDown(Keys.A))
                camera.MoveRelativeRight(-deltaSeconds);
            if (keyboard.IsKeyDown(Keys.D))
                camera.MoveRelativeRight(deltaSeconds);
            if (keyboard.IsKeyDown(Keys.Space))
                camera.MoveUp(deltaSeconds);
            if (keyboard.IsKeyDown(Keys.LeftShift))
                camera.MoveUp(-deltaSeconds);

            // Camera view direction
            if (keyboard.IsKeyDown(Keys.LeftControl))
                camera.LookAt(Vector3.Zero);
            // TODO: rotate/tilt based on pixels/degree (determined by field of view)
            if (captureMouse)
            {
                int screenCenterX = Game.graphics.PreferredBackBufferWidth / 2;
                int screenCenterY = Game.graphics.PreferredBackBufferHeight / 2;
                if (prevCaptureMouse)
                {

                    float deltaX = Mouse.GetState().X - screenCenterX;
                    float deltaY = Mouse.GetState().Y - screenCenterY;

                    float yaw = -deltaX * MathHelper.Pi / 600;
                    float pitch = -deltaY * MathHelper.Pi / 600;

                    camera.Rotate(yaw, pitch, 0);
                }
                Mouse.SetPosition(screenCenterX, screenCenterY);
            }

            // Camera zoom
            float scrollTicks = 120;
            int scroll = Mouse.GetState().ScrollWheelValue;
            float scale = 1;
            if (scroll > 0)
                scale = (scroll / scrollTicks / 10) + 1;
            else if (scroll < 0)
                scale = 1.0f / (Math.Abs(scroll / scrollTicks / 10) + 1);
            camera.Scale = scale;

            prevKeyboard = keyboard;
        }
    }
}
